package com.example.jiemian;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.gxyk.adapter.GridUpPicAdapter;
import com.gxyk.utils.CameraUtilsLister;
import com.gxyk.utils.DialogUtil;
import com.gxyk.utils.ImageUtil;
import com.gxyk.utils.ToastUtil;
import com.gxyk.view.GrildViewForScrollView;
import com.gxyk.view.RoundImageView;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ImageAvtivity extends Activity implements OnPageChangeListener {

	private ViewPager viewPager;
	private ArrayList<ImageView> images;
	private ArrayList<ImageView> dots;	
	private int[] imgIdArray;
	private int oldPosition = 0;// 记录上一次点的位�?
	private int currentItem; // 当前页面

	private final int AUTO_MSG = 1;
	private final int HANDLE_MSG = AUTO_MSG + 1;
	private static final int PHOTO_CHANGE_TIME = 3000;// 定时变量
	private int index = 0;
	private RoundImageView image_image;
	private Button XIANGCE;
	private CameraUtilsLister camera;
	
	private Handler mHandler = new Handler() {

		public void handleMessage(Message msg) {
			switch (msg.what) {
			case AUTO_MSG:
				viewPager.setCurrentItem(index);// 收到消息后设置当前要显示的图�?
				index++;
				mHandler.sendEmptyMessageDelayed(AUTO_MSG, PHOTO_CHANGE_TIME);
				if (index == 5) {
					index = 0;
				}
				break;
			case HANDLE_MSG:
				mHandler.sendEmptyMessageDelayed(AUTO_MSG, PHOTO_CHANGE_TIME);			
				break;
			default:
				break;
			}
		};
	};
private GrildViewForScrollView gv_upload_pic;
GridUpPicAdapter gridAdapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.imageactiviry);

		ViewGroup group = (ViewGroup) findViewById(R.id.viewGroup);
		viewPager = (ViewPager) findViewById(R.id.viewPager);
		image_image=(RoundImageView) findViewById(R.id.image_image);
		XIANGCE=(Button) findViewById(R.id.XIANGCE);
		gv_upload_pic=(GrildViewForScrollView) findViewById(R.id.gv_upload_pic);
		gv_upload_pic.setSelector(new ColorDrawable(Color.TRANSPARENT));
		gridAdapter = new GridUpPicAdapter(ImageAvtivity.this,"920316");
		gv_upload_pic.setAdapter(gridAdapter);
		// 载入图片资源ID
		imgIdArray = new int[] { R.drawable.lyf, R.drawable.hl, R.drawable.zly,
				R.drawable.liushishi, R.drawable.zly };
		images = new ArrayList<ImageView>();
		for (int i = 0; i < imgIdArray.length; i++) {
			ImageView imageView = new ImageView(this);
			imageView.setBackgroundResource(imgIdArray[i]);			
			images.add(imageView);
		}

		dots = new ArrayList<ImageView>();
		for (int i = 0; i < imgIdArray.length; i++) {
			ImageView point_yuan = new ImageView(this);
			point_yuan.setLayoutParams(new LayoutParams(10, 10));
			if (i == 0) {
				point_yuan.setBackgroundResource(R.drawable.point_selected);
				dots.add(point_yuan);
			} else {
				point_yuan.setBackgroundResource(R.drawable.noml_point);
				dots.add(point_yuan);
			}		
		}		
		viewPager.setAdapter(new MyAdapter());		
		viewPager.setOnPageChangeListener(this);
		viewPager.setCurrentItem(currentItem);
		mHandler.sendEmptyMessageDelayed(AUTO_MSG, PHOTO_CHANGE_TIME);
		XIANGCE.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				camera = new CameraUtilsLister(ImageAvtivity.this);
				OnClickListener camera_lister = camera.camera_lister;
				String[] args = { "拍照", "相册" };
				DialogUtil.showSelectDialog(ImageAvtivity.this,"请选择", args, 0, camera_lister);
				
			}
		});
		gv_upload_pic.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				long hs = gridAdapter.getItemId(arg2);
				if (gridAdapter.getItemId(arg2) == arg2) {
					// 开始拍照										
						if (map_list.size() < 6) {
							camera = new CameraUtilsLister(
									ImageAvtivity.this);
							OnClickListener camera_lister = camera.camera_lister;
							String[] args = { "拍照", "相册" };
							DialogUtil.showSelectDialog(ImageAvtivity.this,
									"请选择", args, 0, camera_lister);
						} else {
							ToastUtil.showToast(ImageAvtivity.this,
									"最多上传6张图片");
						}
					}
				}
			
		});
	}
	/**
	 * 用户头像
	 * */
	private List<File> faceFileList = new ArrayList<File>();
	List<Bitmap> map_list = new ArrayList<Bitmap>();
	private List<String> image;
	/**
	 * 用户头像
	 * */
	private File userfaceFile;
	public static String SDCARD = Environment.getExternalStorageDirectory()
			+ "/Android/data/evmanager/iamge/";
	public Uri originalUri;
	private static final int TAKE_PICTURE = 0;
	private static final int CHOOSE_PICTURE = 1;
	private static final int PHOTOSIZE = 1024 * 1024;
	private Bitmap bitmap;
	String name;
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);		
		name = String.valueOf(System.currentTimeMillis());
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case TAKE_PICTURE:
				bitmap = camera.getRightBitmap();
				if (bitmap != null) {
					map_list.add(bitmap);//多张图片上传，显示用的集合
					image_image.setImageBitmap(bitmap);
					userfaceFile = new File(SDCARD + name + ".jpg");
					faceFileList.add(userfaceFile);//多张图片上传，显示用的集合
					ImageUtil.savePhotoToSDCard(bitmap, SDCARD, name);
					gridAdapter.setImagers(map_list);
				} else {
					ToastUtil.showToast(ImageAvtivity.this,
							"保存照片失败,请稍后重试");
				}
				break;

			case CHOOSE_PICTURE:
				// 照片的原始资源地址
				originalUri = data.getData();
				originalUri = data.getData();
				bitmap = camera.getRightBitmap(originalUri);
				// 打印出图片的宽和高
				if (bitmap != null) {
					map_list.add(bitmap);//多张图片上传，显示用的集合
					image_image.setImageBitmap(bitmap);
					userfaceFile = new File(SDCARD + name + ".jpg");
					faceFileList.add(userfaceFile);//多张图片上传，显示用的集合
					ImageUtil.savePhotoToSDCard(bitmap, SDCARD, name);
					gridAdapter.setImagers(map_list);
				} else {
					ToastUtil.showToast(ImageAvtivity.this,"保存照片失败,请稍后重试");
				}
				break;
			default:
				break;
			}
		}
		
		
	}
	public class MyAdapter extends PagerAdapter {

		@Override
		public int getCount() {
	
			return images.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			
			((ViewPager) container).removeView(images.get(position));
		}

		/**
		 * 载入图片进去，用当前的position 除以 图片数组长度取余数是关键
		 */
		@Override
		public Object instantiateItem(View container, int position) {
			
			((ViewGroup) container).addView(images.get(position));
			return images.get(position);
		}

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int arg0) {
         System.out.println(arg0+"haudonghuahudoing");
		dots.get(arg0).setBackgroundResource(R.drawable.point_selected);
		dots.get(oldPosition).setBackgroundResource(R.drawable.noml_point);
		oldPosition = arg0;
		currentItem = arg0;		

	}
}
